import csv

IN_FILE_PATH = '学生个人课表-列表展示导出.CSV'  # 修改成输入文件的路径

def periodSheet(period, mode):
    if mode == 'start':
        if period == 1:
            return '8:15'
        if period == 2:
            return '9:05'
        if period == 3:
            return '10:10'  # '10:30'
        if period == 4:
            return '11:00'  # '11:20'
        if period == 5:
            return '14:45'
        if period == 6:
            return '15:35'
        if period == 7:
            return '16:30'
        if period == 8:
            return '17:20'
        if period == 9:
            return '19:30'
        if period == 10:
            return '20:25'
        if period == 11:
            return '21:30'
        if period == 12:
            return '22:15'
    elif mode == 'end':
        if period == 1:
            return '9:00'
        if period == 2:
            return '9:50'
        if period == 3:
            return '10:55'  # '11:15'
        if period == 4:
            return '11:45'  # '12:05'
        if period == 5:
            return '15:30'
        if period == 6:
            return '16:20'
        if period == 7:
            return '17:15'
        if period == 8:
            return '18:05'
        if period == 9:
            return '20:15'
        if period == 10:
            return '21:10'
        if period == 11:
            return '22:10'
        if period == 12:
            return '23:00'

with open(IN_FILE_PATH, mode='r', encoding='GBK') as inF, \
     open('schedule_out.csv', mode='a', newline='', encoding='GBK') as outF:
    csvReader = csv.reader(inF)
    courseNameCol = 0
    timeCol = 6
    locationCol = 7
    dateCol = 8
    # 校准列索引
    for row in csvReader:
        for index, i in enumerate(row):
            if i == '课程名称':
                courseNameRow = index
            elif i == '节次':
                timeRow = index
            elif i == '上课地点':
                LocationRow = index
            elif i == '排课日期':
                dateCol = index
        break
    csvWriter = csv.writer(outF)
    for row in csvReader:
        if row[0] == '课程名称':
            continue
        curCourseName = row[courseNameCol]
        curLocation = row[locationCol]
        periodStart = periodSheet(int(row[timeRow][0:2]), 'start')
        periodEnd = periodSheet(int(row[timeRow][-2] + row[timeRow][-1]), 'end')
        curDate = row[dateCol].split('-')
        curCourseStartTime = f'{curDate[0]}年{curDate[1]}月{curDate[2]}日{periodStart}'
        curCourseEndTime = f'{curDate[0]}年{curDate[1]}月{curDate[2]}日{periodEnd}'
        csvWriter.writerow([curCourseStartTime, curCourseEndTime, curCourseName, curLocation])
